/* MIT License
 *
 * Copyright (c) 2012 Reuben Smith, Miami University
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.droidlike.vsig;


import com.jme3.math.Vector3f;
import java.util.ArrayList;


/**
 * RoadNode.
 *
 * @author smithre5
 */
public class RoadNode
{
    // smithre5: The up vec is only stored here because it can't be determined
    // from anything. Yet?
    /**
     * Creates a logical node for the road graph.
     *
     * @param position  the location of the logical node
     * @param up        the up vector of the logical node
     * @param lanes     the number of lanes
     * @param laneWidth the lane width
     *
     * @return a logical node if successful; null otherwise
     */
    public static RoadNode create(Vector3f position, Vector3f up, int lanes, float laneWidth)
    {
        if (position == null || up == null || lanes < 1 || laneWidth <= 0f) {
            return null;
        }

        return new RoadNode(
                position,
                up.normalize(),
                (int) Math.abs(lanes),
                Math.abs(laneWidth));
    }
    
    
    /**
     * @return the direction of this node
     */
    public Vector3f getDirection()
    {
        return direction.clone();
    }


    /**
     * Returns the physical position of the center of the Nth lane. Positive lane
     * numbers are on the dominant side of the road and negative numbers are on
     * the opposing side. Usually the user will want lane 1 or -1.
     *
     * @param lane      the lane desired
     *
     * @return a vector representing the center of the Nth lane if successful;
     *         null otherwise
     */
    public Vector3f getLanePosition(int lane)
    {
        if (Math.abs(lane) > lanes || direction == null) {
            return null;
        }

        float laneScale = RIGHT_DOMINANT ? lane * laneWidth : lane * -laneWidth;
        Vector3f lanePos = direction.cross(up).normalizeLocal().multLocal(laneScale);
        lanePos.addLocal(position);

        return lanePos;
    }
    
    
    /**
     * @return the number of lanes per side
     */
    public int getLanes()
    {
        return lanes;
    }


    /**
     * @return a copy of the node's position vector
     */
    public Vector3f getPosition()
    {
        return position.clone();
    }
    
    
    public Object[][] getTableInfo(RoadGraph graph)
    {
        ArrayList<Object[]> tableItems = new ArrayList<Object[]>();
        
        tableItems.add(new Object[]{ "Type", "RoadNode" });
        tableItems.add(new Object[]{ "Position", position.toString() });
        tableItems.add(new Object[]{ "Direction", direction.toString() });
        tableItems.add(new Object[]{ "Normal", up.toString() });
        tableItems.add(new Object[]{ "Lanes", lanes });
        tableItems.add(new Object[]{ "Lane Width", laneWidth });
        
        Vector3f[] edges = graph.getEdges(position);
        for (int i = edges.length; --i >= 0;) {
            tableItems.add(new Object[]{ "Edge " + (edges.length - i), edges[i] });
        }
        
        return tableItems.toArray(new Object[tableItems.size()][2]);
    }


    /**
     * @return a copy of the node's up vector
     */
    public Vector3f getUp()
    {
        return up.clone();
    }
    
    
    public void setDirection(Vector3f direction)
    {
        this.direction = direction == null ? Vector3f.ZERO : direction.normalize();
    }


    /**
     * Default constructor.
     *
     * @param position  the location of this node
     * @param direction the direction of this node
     * @param up        the up vector of this node
     * @param lane      the number of lanes
     * @param laneWidth the width for each lane of this node
     */
    private RoadNode(Vector3f position, Vector3f up, int lanes, float laneWidth)
    {
        this.position = position;
        this.direction = Vector3f.ZERO;
        this.up = up;
        this.lanes = lanes;
        this.laneWidth = laneWidth;
    }


    // Defines which side of the road is the "forward" direction.
    public static final boolean RIGHT_DOMINANT = true;
    private Vector3f position;
    private Vector3f direction;
    private Vector3f up;
    private int lanes;
    private float laneWidth;
}

